package io.sqrtqiezi.spark.udf

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

object UDFSample {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .master("local")
      .appName("udf sample")
      .getOrCreate()

    val df = spark.range(5).toDF("num")
    df.show

    def power3(number:Double):Double = number * number * number

    val power3udf = udf(power3(_: Double): Double)

    df.select(power3udf(col("num"))).show

    spark.udf.register("power3", power3(_:Double):Double)
    df.selectExpr("power3(num)").sort(col("num").desc).show(2)

    println(df.selectExpr("power3(num)").sort(col("num").desc).explain)
  }
}
